package com.gouwudai.gobbs.test;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.dbunit.DatabaseUnitException;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.operation.DatabaseOperation;

/**
 * <pre>
 *  Title: DBUnit
 *  Description: DBUnit操作数据库模版  
 * </pre>
 * 
 * @author <a href="mailto:littcai@hotmail.com">空心大白菜</a>
 * @date 2007-4-7
 * @version 1.0
 * 
 * <pre>
 * 修改记录
 *    修改后版本:     修改人：  修改日期:     修改内容: 
 * </pre>
 */
public class DBUnitTemplate
{
    private DataSource dataSource;

    private IDatabaseConnection connection;

    public void setDataSource(DataSource dataSource) throws SQLException
    {

        this.dataSource = dataSource;
        // ”schema”是你数据库的schema的名字，试验的时候老是抛出AmbiguousTableNameException，后来在Dbunit的官方FAQ中找到，必须要用schema，而且如果你用的Oracle，必须要大写
        connection = new DatabaseConnection(this.dataSource.getConnection(),
                "schema");
    }

    public void execute(String path, DatabaseOperation oper)
            throws FileNotFoundException, IOException, DatabaseUnitException,
            SQLException
    {

        IDataSet dataset = new FlatXmlDataSet(this.getClass().getResource(path));
        oper.execute(connection, dataset);
    }

}
